package com.stadium.controller;

import java.util.List;
import java.util.UUID;

import com.stadium.common.BaseResponse;
import com.stadium.common.exception.RRException;
import com.stadium.dto.GamePlayerDto;
import com.stadium.dto.ImportPhotoDto;
import com.stadium.dto.SysUserDto1;
import com.stadium.entity.TeamRecord;
import com.stadium.service.GamePlayerSV;
import com.stadium.service.RTeamUserSV;
import com.stadium.vo.GamePlayerVo;
import com.stadium.vo.ImportPhotoVo;
import com.stadium.vo.ImportUserVo;
import com.stadium.vo.searchVo.GamePlayerSearchVo;
import io.micrometer.core.instrument.util.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 球员表
 *
 * @author wsl
 * @email
 * @date 2021-05-11 14:58:57
 */
@RestController
@RequestMapping("/api/gameplayer")
@Api(tags = "球员表")
public class GamePlayerCL{
    private Logger logger = LoggerFactory.getLogger(this.getClass());
    @Autowired
    private GamePlayerSV gamePlayerSV;
    @Autowired
    private RTeamUserSV rTeamUserSV;

    @ApiOperation(value = "保存或修改")
    @PostMapping(value = "/v1/save")
    public BaseResponse save(@Validated GamePlayerVo gamePlayerVo) {
        try {
            if (gamePlayerVo.getId() == null ||gamePlayerVo.getId() == ""){
                    gamePlayerSV.save(gamePlayerVo);
            }else{
                    gamePlayerSV.update(gamePlayerVo);
            }
            return BaseResponse.ok();

        }catch (RRException e) {
            e.printStackTrace();
            return BaseResponse.error(e.getMsg());
        }
        catch (Exception e) {
            e.printStackTrace();
            return BaseResponse.error();
        }
    }

    @ApiOperation(value = "修改球员信息卡")
    @PostMapping(value = "/v1/update")
    public BaseResponse updatePlayer(@Validated GamePlayerVo gamePlayerVo) {
        try {
            gamePlayerSV.updatePlayer(gamePlayerVo);
            return BaseResponse.ok();
        } catch (Exception e) {
            e.printStackTrace();
            return BaseResponse.error();
        }
    }

    @ApiOperation(value = "删除")
    @PostMapping(value = "/v1/delete")
    @ApiImplicitParams({@ApiImplicitParam(name="id",value="id")})
    public BaseResponse delete(@RequestParam(name = "id", required = true)String id) {
        try {
            gamePlayerSV.delete(id);
            return BaseResponse.ok();
        } catch (Exception e) {
            e.printStackTrace();
            return BaseResponse.error();
        }
    }

    /*@ApiOperation(value = "批量删除")
    @PostMapping(value = "/v1/deleteBatch")
    @ApiImplicitParams({@ApiImplicitParam(name="ids",value="ids")})
    public BaseResponse deleteBatch(@RequestParam(name = "ids", required = true)String[] ids) {
        try {
            gamePlayerSV.deleteBatch(ids);
            return BaseResponse.ok();
        } catch (Exception e) {
            e.printStackTrace();
            return BaseResponse.error();
        }
    }*/

    @ApiOperation(value = "获得列表")
    @GetMapping(value = "/v1/query")
    public BaseResponse<List<GamePlayerDto>> query(@Validated GamePlayerSearchVo searchVo) {
        try {
            if (StringUtils.isNotEmpty(searchVo.getSearchValue())) {
                searchVo.setSearchValue("%" + searchVo.getSearchValue() + "%");
            }
            List<GamePlayerDto> queryList = gamePlayerSV.query(searchVo);
            int total = gamePlayerSV.getCount(searchVo);
            return BaseResponse.ok(queryList, total);
        } catch (Exception e) {
            logger.error("日志失败！", e);
            return BaseResponse.error();
        }
    }

    @ApiOperation(value = "获得详情", notes = "获得详情")
    @GetMapping(value = "/v1/get")
    @ApiImplicitParams({@ApiImplicitParam(name="id",value="id")})
    public BaseResponse<GamePlayerDto> get(@RequestParam(name = "id", required = true)String id) {
        try {
                GamePlayerDto queryData = gamePlayerSV.get(id);
            return BaseResponse.ok(queryData);
        } catch (Exception e) {
            logger.error("日志失败！", e);
            return BaseResponse.error();
        }
    }

    @ApiOperation(value = "球员信息卡", notes = "球员信息卡")
    @GetMapping(value = "/v1/getDetail")
    @ApiImplicitParams({@ApiImplicitParam(name="id",value="id")})
    public BaseResponse<GamePlayerDto> getByUserId(@RequestParam(name = "id", required = true)String id) {
        try {
            GamePlayerDto queryData = gamePlayerSV.getByUserId(id);
            return BaseResponse.ok(queryData);
        } catch (Exception e) {
            logger.error("日志失败！", e);
            return BaseResponse.error();
        }
    }


    @ApiOperation(value = "球队导入球员")
    @PostMapping(value = "/v1/importUserToTeam")
    public BaseResponse importUserToTeam(ImportUserVo importUserVo) {
        try {
            BaseResponse result = gamePlayerSV.importUserToTeam(importUserVo);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            return BaseResponse.error();
        }
    }

    @ApiOperation(value = "获得球队队员", notes = "获得球队队员")
    @GetMapping(value = "/v1/getPlayersByTeamId")
    @ApiImplicitParams({@ApiImplicitParam(name="teamId",value="teamId")})
    public BaseResponse<List<SysUserDto1>> getPlayersByTeamId(@RequestParam(name = "teamId", required = true)String teamId) {
        try {
            List<SysUserDto1> queryData = rTeamUserSV.getPlayersByTeamId(teamId);
            return BaseResponse.ok(queryData);
        } catch (Exception e) {
            logger.error("日志失败！", e);
            return BaseResponse.error();
        }
    }

    @ApiOperation(value = "球员导入照片--生成人脸特征值")
    @PostMapping(value = "/v1/importPhoto")
    public BaseResponse<ImportPhotoDto> importPhoto(ImportPhotoVo importPhotoVo) {
        try {
            String fileName = new String(importPhotoVo.getMultipartFile().getOriginalFilename().getBytes("UTF-8"));
            String fileType = fileName.substring(fileName.lastIndexOf(".") + 1, fileName.length()).toLowerCase();
            if (fileType.equals("zip")) {
                ImportPhotoDto importPhotoDto = gamePlayerSV.importPhoto(importPhotoVo);
                return BaseResponse.ok(importPhotoDto);
            } else {
                return BaseResponse.error("请上传zip文件，您上传的文件不是zip文件");
            }
        } catch (Exception e) {
            logger.error("导入图片失败", e);
            return BaseResponse.error(e.getMessage());
        }
    }

    /*@ApiOperation(value = "球员战绩")
    @GetMapping(value = "/v1/playerRecord")
    @ApiImplicitParams({@ApiImplicitParam(name="userId",value="userId")})
    public BaseResponse<TeamRecord> TeamRecord(@RequestParam(name = "userId", required = true)String userId) {
        try {
            TeamRecord queryList = gamePlayerSV.palyerRecorg(userId);
            return BaseResponse.ok(queryList);
        } catch (Exception e) {
            logger.error("日志失败！", e);
            return BaseResponse.error();
        }
    }*/
    @ApiOperation(value = "获得列表--携带人脸特征值")
    @GetMapping(value = "/v1/queryPlayerAndDface")
    public BaseResponse<List<GamePlayerDto>> queryPlayerAndDface(@Validated GamePlayerSearchVo searchVo) {
        try {
            if (StringUtils.isNotEmpty(searchVo.getSearchValue())) {
                searchVo.setSearchValue("%" + searchVo.getSearchValue() + "%");
            }
            if (StringUtils.isEmpty(searchVo.getAuditStatus())) {
                searchVo.setAuditStatus("1");
            }
            List<GamePlayerDto> queryList = gamePlayerSV.queryPlayerAndDface(searchVo);
            int total = gamePlayerSV.getPlayerCount(searchVo);
            return BaseResponse.ok(queryList, total);
        } catch (Exception e) {
            logger.error("日志失败！", e);
            return BaseResponse.error();
        }
    }
}
